package com.cyjt.operation.fragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.cyjt.operation.R;
import com.cyjt.operation.adapter.DynamicNodesStatusAdapter;
import com.cyjt.operation.base.AppContext;
import com.cyjt.operation.base.HandlerMessageCodes;
import com.cyjt.operation.bean.DynamicNodesStatusInfo;
import com.cyjt.operation.ui.NodesStatusListActivity;
import com.cyjt.operation.view.ActionBarLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 显示动态节点状态列表的Fragment，附属于NodesStatusListActivity的ViewPager
 * 
 * @author LTP
 *
 */
public class DynamicNodesStatusFragment extends Fragment {
	private View view;
	/** 显示动态节点状态列表的ListView */
	private ListView listView_dynamic_nodes_status_list;
	/** 下拉刷新控件 */
	private SwipeRefreshLayout mSwipeRefreshLayout;
	/** 当动态节点状态列表为空的提示 */
	private TextView textView_dynamic_nodes_status_hint;
	/** 全部状态下的动态节点的列表 */
	private List<DynamicNodesStatusInfo> dynamicNodesStatusInfoList;
	/** 正常状态下的动态节点的列表 */
	private List<DynamicNodesStatusInfo> nomalDynamicNodesStatusInfolList = new ArrayList<DynamicNodesStatusInfo>();
	/** 异常状态下的动态节点的列表 */
	private List<DynamicNodesStatusInfo> abnomalDynamicNodesStatusInfolList = new ArrayList<DynamicNodesStatusInfo>();
	private DynamicNodesStatusAdapter mDynamicNodesStatusAdapter;

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HandlerMessageCodes.FETCH_DYNAMIC_NODES_STATUS_LIST_SUCCEED:
				ActionBarLayout.isShowProgressBar(false);// 加载成功时，使标题上的进度圈不显示
				dynamicNodesStatusInfoList = (List<DynamicNodesStatusInfo>) msg.obj;
				separateDynamicNodeListData();
				if ("查看正常节点".equals(NodesStatusListActivity.getButtonText())) {
					loadAbnormalDynamicNodesStatusList();
				} else {
					loadNomalDynamicNodesStatusList();
				}
				break;
			case HandlerMessageCodes.FETCH_DYNAMIC_NODES_STATUS_LIST_FAILED:
				ActionBarLayout.isShowProgressBar(false);
				Toast.makeText(getActivity(), "从服务器获取的动态节点信息为空", Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_dynamic_node_status, container, false);
		initView();// 从服务器获取动态节点状态的数据
		return view;
	}

	/**
	 * 从服务器获取动态节点状态的数据
	 */
	private void feachDynamicNodesStatus() {
		AppContext.getInstance().getVolleyTools().fetchDynamicNodesStatusList(handler,
				AppContext.getInstance().getUser());
	}

	/**
	 * 控件初始化
	 */
	private void initView() {
		textView_dynamic_nodes_status_hint = (TextView) view.findViewById(R.id.textView_dynamic_nodes_status_hint);
		listView_dynamic_nodes_status_list = (ListView) view.findViewById(R.id.listView_dynamic_nodes_status_list);
		listView_dynamic_nodes_status_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO 动态节点列表ListView的点击事件
				DialogFragmentForDeviceStatusDetails mDeviceStatusDetails = new DialogFragmentForDeviceStatusDetails();
				mDeviceStatusDetails.setStyle(DialogFragment.STYLE_NO_TITLE,
						android.R.style.Theme_Holo_Dialog_NoActionBar_MinWidth);
				Bundle args = new Bundle();
				DynamicNodesStatusInfo dynamicNodesStatusInfo;
				if ("查看正常节点".equals(NodesStatusListActivity.getButtonText())) {
					dynamicNodesStatusInfo = abnomalDynamicNodesStatusInfolList.get(position);
				} else {
					dynamicNodesStatusInfo = nomalDynamicNodesStatusInfolList.get(position);
				}
				String dynamicNodesDetailInfoString = "节点编号：" + dynamicNodesStatusInfo.getNfcCode() + "\n\n基站编码："
						+ dynamicNodesStatusInfo.getBasestationCode() + "\n\n所在路段：" + dynamicNodesStatusInfo.getTitle()
						+ "\n\n节点位置：" + dynamicNodesStatusInfo.getCrossing() + "\n\n所在车道："
						+ dynamicNodesStatusInfo.getDriveway() + "\n\n节点状态："
						+ dynamicNodesStatusInfo.getStatusToString();
				// args.putString("title", "动态节点详情");// 传递Dialog的标题
				args.putString("text", dynamicNodesDetailInfoString);
				mDeviceStatusDetails.setArguments(args);
				mDeviceStatusDetails.show(getActivity().getFragmentManager(), "dynamicNodesDetailInfo");
			}
		});
		mSwipeRefreshLayout = (SwipeRefreshLayout) view
				.findViewById(R.id.swiprrefreshLayout_for_dynamic_nodes_status_list);
		mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_red_dark, android.R.color.holo_green_dark,
				android.R.color.holo_blue_dark, android.R.color.holo_purple);
		mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				ActionBarLayout.isShowProgressBar(true);
				if (dynamicNodesStatusInfoList != null) {
					dynamicNodesStatusInfoList.clear();
				}
				nomalDynamicNodesStatusInfolList.clear();
				abnomalDynamicNodesStatusInfolList.clear();
				feachDynamicNodesStatus();
				mSwipeRefreshLayout.setRefreshing(false);
			}
		});
	}

	/**
	 * 分离动态节点列表中正异常状态的数据
	 */
	private void separateDynamicNodeListData() {
		Collections.sort(dynamicNodesStatusInfoList);
		for (DynamicNodesStatusInfo dynamicNodesStatusInfo : dynamicNodesStatusInfoList) {
			if (dynamicNodesStatusInfo.getStatus() == 1) {
				// 当静态节点的状态为1即正常时，装载进normalNodesStatusInfoList
				nomalDynamicNodesStatusInfolList.add(dynamicNodesStatusInfo);
			} else if (dynamicNodesStatusInfo.getStatus() == 2) {
				// 当静态节点的状态为2即异常时，装载进abnormalNodesStatusInfoList
				abnomalDynamicNodesStatusInfolList.add(dynamicNodesStatusInfo);
			}
		}
	}

	/**
	 * 加载异常动态节点的数据
	 */
	public void loadAbnormalDynamicNodesStatusList() {
		textView_dynamic_nodes_status_hint.setText("");
		if (abnomalDynamicNodesStatusInfolList.isEmpty()) {
			textView_dynamic_nodes_status_hint.setText("异常动态节点列表为空");
		}
		mDynamicNodesStatusAdapter = new DynamicNodesStatusAdapter(getActivity(),
				R.layout.list_view_item_for_dynamic_nodes_status, abnomalDynamicNodesStatusInfolList);
		listView_dynamic_nodes_status_list.setAdapter(mDynamicNodesStatusAdapter);
	}

	/**
	 * 加载正常动态节点的数据
	 */
	public void loadNomalDynamicNodesStatusList() {
		textView_dynamic_nodes_status_hint.setText("");
		if (nomalDynamicNodesStatusInfolList.isEmpty()) {
			textView_dynamic_nodes_status_hint.setText("正常动态节点列表为空");
		}
		mDynamicNodesStatusAdapter = new DynamicNodesStatusAdapter(getActivity(),
				R.layout.list_view_item_for_dynamic_nodes_status, nomalDynamicNodesStatusInfolList);
		listView_dynamic_nodes_status_list.setAdapter(mDynamicNodesStatusAdapter);
	}
}
